//
//  StarGroupView.m
//  MyPopStar
//
//  Created by tarena on 16/1/6.
//  Copyright © 2016年 ssnb. All rights reserved.
//

#import "StarGroupView.h"
#import "Fireworks.h"
@interface StarGroupView ()<StarViewDelegate>
@property (nonatomic,strong) NSMutableArray *viewGroup;
@end

@implementation StarGroupView
mdealloc
#pragma mark -- 添加start对象
-(void)addStarsModelGroup:(allStarArray)starsModelGroup{
    self.viewGroup = [[NSMutableArray alloc]initWithCapacity:10];
    for (columnArray arr in starsModelGroup) {
        NSMutableArray *columnView=[[NSMutableArray alloc]initWithCapacity:10];
        for (StarModel *model in arr) {
            StarView *row = [[StarView alloc]initWithFrame:model.mFrame andStarType:model.starModelType andPoint:model.starPoint];
            row.delegate=self;
            [columnView addObject:row];
            [self addSubview:row];
        }
        [self.viewGroup addObject:columnView];
    }
}

#pragma mark -- 更新界面
-(void)updateViewWith:(NSMutableArray *)starsModelGroup{
    for (NSInteger i =self.viewGroup.count-1 ;i>=0;i--) {
        if ([[self.viewGroup objectAtIndex:i] count]==0) {
            [self.viewGroup removeObjectAtIndex:i];
        }
    }
    [self updateAddress:starsModelGroup];
}

-(void)updateViewWithd:(NSMutableArray *)starsModelGroup andRemoveStarModelGroup:(NSMutableArray*)arr{

    NSMutableDictionary *dicView= [[NSMutableDictionary alloc]init];

    for (StarModel *mo in arr) {
        StarView *view =[[self.viewGroup objectAtIndex:mo.starPoint.column] objectAtIndex:mo.starPoint.row];
        [self fire:view.frame :view.dielImgName];
        [view remove];
        [dicView setObject:view forKey:[NSNumber numberWithInteger:(mo.starPoint.column*10+mo.starPoint.row)]];
    }
    NSArray *key = [dicView allKeys];
    for (NSNumber *nub in key) {
        [[self.viewGroup objectAtIndex:(nub.integerValue/10)] removeObject:[dicView objectForKey:nub]];
    }
    [self updateAddress:starsModelGroup];
    [self.delegate starGroupView:self updateOver:YES];
}

-(void)updateAddress:(NSMutableArray *)starsModelGroup{
    for (int i = 0; i<self.viewGroup.count; i++) {//进行更新位置
        NSMutableArray *colum = [self.viewGroup objectAtIndex:i];
        for (int j = 0 ; j<colum.count;j++ ) {
            StarModel *model=[[starsModelGroup objectAtIndex:i] objectAtIndex:j];
            [[colum objectAtIndex:j] updateStatus:model.mFrame andPoint:model.starPoint];
        }
    }
}

-(void)fire:(CGRect)frame :(NSString *)name{
    CAEmitterLayerView *fireView = [[Fireworks alloc] initWithFrame:frame];
    fireView.fireImgName=name;
    [self addSubview:fireView];
    [fireView show];
}

-(void)animationWithArray:(NSMutableArray *)array andStatus:(BOOL)isStart{
    for (StarModel *mo in array) {
        StarView *view =[[self.viewGroup objectAtIndex:mo.starPoint.column] objectAtIndex:mo.starPoint.row];
        if (isStart) {
            [view stopClickAnimation];
        }else{
            [view starClickAnimation];
        }
    }
}
#pragma mark -- 数据清理
-(void)Over{
    WeakSelf(content);
    dispatch_queue_t queue = dispatch_queue_create("ssnb", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [content cleans];
        });
        
        [NSThread sleepForTimeInterval:3];
        dispatch_async(dispatch_get_main_queue(), ^{
            [content.delegate starGroupView:content overAnimationOver:YES];
        });
    });
}
-(void)cleans{
    for (int i = 0; i<self.viewGroup.count; i++) {//进行更新位置
        NSMutableArray *colum = [self.viewGroup objectAtIndex:i];
        for (int j = 0 ; j<colum.count;j++ ) {
           StarView *view = [colum objectAtIndex:j] ;
            [self fire:view.frame :view.dielImgName];
            [view remove];
        }
    }
    self.viewGroup =nil;
}

-(void)clean{
    for(StarView *view in self.subviews)
    {
        [view remove];
    }
    self.viewGroup = nil;
}
#pragma mark --StarViewDelegate
-(void)star:(StarView *)star touchPoint:(StarPoint)point{
    if (self.delegate!=nil) {
        [self.delegate starGroupView:self touchPoint:point];
    }
}

@end
